<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>plaintextspellchecker.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_plaintextspellchecker.js.html">plaintextspellchecker.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Plain text spell checker implementation.
<a name="line17"></a> *
<a name="line18"></a> * @see ../demos/plaintextspellchecker.html
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.ui.PlainTextSpellChecker&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line24"></a>goog.require(&#39;goog.dom&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom.a11y&#39;);
<a name="line26"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line27"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line28"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.KeyHandler&#39;);
<a name="line30"></a>goog.require(&#39;goog.events.KeyHandler.EventType&#39;);
<a name="line31"></a>goog.require(&#39;goog.style&#39;);
<a name="line32"></a>goog.require(&#39;goog.ui.AbstractSpellChecker&#39;);
<a name="line33"></a>goog.require(&#39;goog.ui.AbstractSpellChecker.AsyncResult&#39;);
<a name="line34"></a>goog.require(&#39;goog.ui.Component.EventType&#39;);
<a name="line35"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>/**
<a name="line39"></a> * Plain text spell checker implementation.
<a name="line40"></a> *
<a name="line41"></a> * @param {goog.spell.SpellCheck} handler Instance of the SpellCheckHandler
<a name="line42"></a> *     support object to use. A single instance can be shared by multiple
<a name="line43"></a> *     editor components.
<a name="line44"></a> * @param {goog.dom.DomHelper} opt_domHelper Optional DOM helper.
<a name="line45"></a> * @constructor
<a name="line46"></a> * @extends {goog.ui.AbstractSpellChecker}
<a name="line47"></a> */
<a name="line48"></a>goog.ui.PlainTextSpellChecker = function(handler, opt_domHelper) {
<a name="line49"></a>  goog.ui.AbstractSpellChecker.call(this, handler, opt_domHelper);
<a name="line50"></a>
<a name="line51"></a>  /**
<a name="line52"></a>   * Correction UI container.
<a name="line53"></a>   * @type {HTMLDivElement}
<a name="line54"></a>   * @private
<a name="line55"></a>   */
<a name="line56"></a>  this.overlay_ = /** @type {HTMLDivElement} */
<a name="line57"></a>      (this.getDomHelper().createDom(&#39;div&#39;));
<a name="line58"></a>  goog.style.setPreWrap(this.overlay_);
<a name="line59"></a>
<a name="line60"></a>  /**
<a name="line61"></a>   * Bound async function (to avoid rebinding it on every call).
<a name="line62"></a>   * @type {Function}
<a name="line63"></a>   * @private
<a name="line64"></a>   */
<a name="line65"></a>  this.boundContinueAsyncFn_ = goog.bind(this.continueAsync_, this);
<a name="line66"></a>
<a name="line67"></a>  /**
<a name="line68"></a>   * Regular expression for matching line breaks.
<a name="line69"></a>   * @type {RegExp}
<a name="line70"></a>   * @private
<a name="line71"></a>   */
<a name="line72"></a>  this.endOfLineMatcher_ = new RegExp(&#39;(.*)(\n|\r\n){0,1}&#39;, &#39;g&#39;);
<a name="line73"></a>};
<a name="line74"></a>goog.inherits(goog.ui.PlainTextSpellChecker, goog.ui.AbstractSpellChecker);
<a name="line75"></a>
<a name="line76"></a>/**
<a name="line77"></a> * Class name for invalid words.
<a name="line78"></a> * @type {string}
<a name="line79"></a> */
<a name="line80"></a>goog.ui.PlainTextSpellChecker.prototype.invalidWordClassName =
<a name="line81"></a>    goog.getCssName(&#39;goog-spellcheck-invalidword&#39;);
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * Class name for corrected words.
<a name="line85"></a> * @type {string}
<a name="line86"></a> */
<a name="line87"></a>goog.ui.PlainTextSpellChecker.prototype.correctedWordClassName =
<a name="line88"></a>  goog.getCssName(&#39;goog-spellcheck-correctedword&#39;);
<a name="line89"></a>
<a name="line90"></a>/**
<a name="line91"></a> * Class name for correction pane.
<a name="line92"></a> * @type {string}
<a name="line93"></a> */
<a name="line94"></a>goog.ui.PlainTextSpellChecker.prototype.correctionPaneClassName =
<a name="line95"></a>    goog.getCssName(&#39;goog-spellcheck-correctionpane&#39;);
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Number of words to scan to precharge the dictionary.
<a name="line99"></a> * @type {number}
<a name="line100"></a> * @private
<a name="line101"></a> */
<a name="line102"></a>goog.ui.PlainTextSpellChecker.prototype.dictionaryPreScanSize_ = 1000;
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * Size of window. Used to check if a resize operation actually changed the size
<a name="line106"></a> * of the window.
<a name="line107"></a> * @type {goog.math.Size|undefined}
<a name="line108"></a> * @private
<a name="line109"></a> */
<a name="line110"></a>goog.ui.PlainTextSpellChecker.prototype.winSize_;
<a name="line111"></a>
<a name="line112"></a>/**
<a name="line113"></a> * Numeric Id of the element that has focus. 0 when not set.
<a name="line114"></a> *
<a name="line115"></a> * @type {number}
<a name="line116"></a> * @private
<a name="line117"></a> */
<a name="line118"></a>goog.ui.AbstractSpellChecker.prototype.focusedElementId_ = 0;
<a name="line119"></a>
<a name="line120"></a>/**
<a name="line121"></a> * Event handler for listening to events without leaking.
<a name="line122"></a> * @type {goog.events.EventHandler|undefined}
<a name="line123"></a> * @private
<a name="line124"></a> */
<a name="line125"></a>goog.ui.PlainTextSpellChecker.prototype.eventHandler_;
<a name="line126"></a>
<a name="line127"></a>/**
<a name="line128"></a> * The object handling keyboard events.
<a name="line129"></a> * @type {goog.events.KeyHandler|undefined}
<a name="line130"></a> * @private
<a name="line131"></a> */
<a name="line132"></a>goog.ui.PlainTextSpellChecker.prototype.keyHandler_;
<a name="line133"></a>
<a name="line134"></a>/**
<a name="line135"></a> * Creates the initial DOM representation for the component.
<a name="line136"></a> */
<a name="line137"></a>goog.ui.PlainTextSpellChecker.prototype.createDom = function() {
<a name="line138"></a>  this.setElementInternal(this.getDomHelper().createElement(&#39;textarea&#39;));
<a name="line139"></a>};
<a name="line140"></a>
<a name="line141"></a>
<a name="line142"></a>/**
<a name="line143"></a> * Called when the component&#39;s element is known to be in the document.
<a name="line144"></a> */
<a name="line145"></a>goog.ui.PlainTextSpellChecker.prototype.enterDocument = function() {
<a name="line146"></a>  goog.ui.PlainTextSpellChecker.superClass_.enterDocument.call(this);
<a name="line147"></a>
<a name="line148"></a>  this.eventHandler_ = new goog.events.EventHandler(this);
<a name="line149"></a>  this.keyHandler_ = new goog.events.KeyHandler(this.overlay_);
<a name="line150"></a>
<a name="line151"></a>  this.initSuggestionsMenu();
<a name="line152"></a>  this.initAccessibility_();
<a name="line153"></a>};
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/** @inheritDoc */
<a name="line157"></a>goog.ui.PlainTextSpellChecker.prototype.exitDocument = function() {
<a name="line158"></a>  goog.ui.PlainTextSpellChecker.superClass_.exitDocument.call(this);
<a name="line159"></a>
<a name="line160"></a>  if (this.eventHandler_) {
<a name="line161"></a>    this.eventHandler_.dispose();
<a name="line162"></a>    this.eventHandler_ = undefined;
<a name="line163"></a>  }
<a name="line164"></a>  if (this.keyHandler_) {
<a name="line165"></a>    this.keyHandler_.dispose();
<a name="line166"></a>    this.keyHandler_ = undefined;
<a name="line167"></a>  }
<a name="line168"></a>};
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>/**
<a name="line172"></a> * Initializes suggestions menu. Populates menu with separator and ignore option
<a name="line173"></a> * that are always valid. Suggestions are later added above the separator.
<a name="line174"></a> *
<a name="line175"></a> * @protected
<a name="line176"></a> */
<a name="line177"></a>goog.ui.PlainTextSpellChecker.prototype.initSuggestionsMenu = function() {
<a name="line178"></a>  goog.ui.PlainTextSpellChecker.superClass_.initSuggestionsMenu.call(this);
<a name="line179"></a>  this.eventHandler_.listen(/** @type {goog.ui.PopupMenu} */ (this.menu_),
<a name="line180"></a>      goog.ui.Component.EventType.BLUR, this.onCorrectionBlur_);
<a name="line181"></a>};
<a name="line182"></a>
<a name="line183"></a>
<a name="line184"></a>/**
<a name="line185"></a> * Checks spelling for all text and displays correction UI.
<a name="line186"></a> */
<a name="line187"></a>goog.ui.PlainTextSpellChecker.prototype.check = function() {
<a name="line188"></a>  var text = this.getElement().value;
<a name="line189"></a>  this.getElement().readOnly = true;
<a name="line190"></a>
<a name="line191"></a>  // Prepare and position correction UI.
<a name="line192"></a>  this.overlay_.innerHTML = &#39;&#39;;
<a name="line193"></a>  this.overlay_.className = this.correctionPaneClassName;
<a name="line194"></a>  if (this.getElement().parentNode != this.overlay_.parentNode) {
<a name="line195"></a>    this.getElement().parentNode.appendChild(this.overlay_);
<a name="line196"></a>  }
<a name="line197"></a>  goog.style.showElement(this.overlay_, false);
<a name="line198"></a>
<a name="line199"></a>  this.preChargeDictionary_(text);
<a name="line200"></a>};
<a name="line201"></a>
<a name="line202"></a>
<a name="line203"></a>/**
<a name="line204"></a> * Final stage of spell checking - displays the correction UI.
<a name="line205"></a> * @private
<a name="line206"></a> */
<a name="line207"></a>goog.ui.PlainTextSpellChecker.prototype.finishCheck_ = function() {
<a name="line208"></a>  // Show correction UI.
<a name="line209"></a>  this.positionOverlay_();
<a name="line210"></a>  goog.style.showElement(this.getElement(), false);
<a name="line211"></a>  goog.style.showElement(this.overlay_, true);
<a name="line212"></a>
<a name="line213"></a>  var eh = this.eventHandler_;
<a name="line214"></a>  eh.listen(this.overlay_, goog.events.EventType.CLICK, this.onWordClick_);
<a name="line215"></a>  eh.listen(/** @type {goog.events.KeyHandler} */ (this.keyHandler_),
<a name="line216"></a>      goog.events.KeyHandler.EventType.KEY, this.handleOverlayKeyEvent);
<a name="line217"></a>
<a name="line218"></a>  // The position and size of the overlay element needs to be recalculated if
<a name="line219"></a>  // the browser window is resized.
<a name="line220"></a>  var win = goog.dom.getWindow(this.getDomHelper().getDocument()) || window;
<a name="line221"></a>  this.winSize_ = goog.dom.getViewportSize(win);
<a name="line222"></a>  eh.listen(win, goog.events.EventType.RESIZE, this.onWindowResize_);
<a name="line223"></a>
<a name="line224"></a>  goog.ui.PlainTextSpellChecker.superClass_.check.call(this);
<a name="line225"></a>};
<a name="line226"></a>
<a name="line227"></a>
<a name="line228"></a>/**
<a name="line229"></a> * Start the scan after the dictionary was loaded.
<a name="line230"></a> *
<a name="line231"></a> * @param {string} text text to process.
<a name="line232"></a> * @private
<a name="line233"></a> */
<a name="line234"></a>goog.ui.PlainTextSpellChecker.prototype.preChargeDictionary_ = function(text) {
<a name="line235"></a>  this.eventHandler_.listen(this.handler_,
<a name="line236"></a>      goog.spell.SpellCheck.EventType.READY, this.onDictionaryCharged_, true);
<a name="line237"></a>
<a name="line238"></a>  this.populateDictionary(text, this.dictionaryPreScanSize_);
<a name="line239"></a>};
<a name="line240"></a>
<a name="line241"></a>
<a name="line242"></a>/**
<a name="line243"></a> * Loads few initial dictionary words into the cache.
<a name="line244"></a> *
<a name="line245"></a> * @param {goog.events.Event} e goog.spell.SpellCheck.EventType.READY event.
<a name="line246"></a> * @private
<a name="line247"></a> */
<a name="line248"></a>goog.ui.PlainTextSpellChecker.prototype.onDictionaryCharged_ = function(e) {
<a name="line249"></a>  e.stopPropagation();
<a name="line250"></a>  this.eventHandler_.unlisten(this.handler_,
<a name="line251"></a>      goog.spell.SpellCheck.EventType.READY, this.onDictionaryCharged_, true);
<a name="line252"></a>  this.checkAsync_(this.getElement().value);
<a name="line253"></a>};
<a name="line254"></a>
<a name="line255"></a>
<a name="line256"></a>/**
<a name="line257"></a> * Processes the included and skips the excluded text ranges.
<a name="line258"></a> * @return {goog.ui.AbstractSpellChecker.AsyncResult} Whether the spell
<a name="line259"></a> *     checking is pending or done.
<a name="line260"></a> * @private
<a name="line261"></a> */
<a name="line262"></a>goog.ui.PlainTextSpellChecker.prototype.spellCheckLoop_ = function() {
<a name="line263"></a>  for (var i = this.textArrayIndex_; i &lt; this.textArray_.length; ++i) {
<a name="line264"></a>    var text = this.textArray_[i];
<a name="line265"></a>    if (this.textArrayProcess_[i]) {
<a name="line266"></a>      var result = this.processTextAsync(this.overlay_, text);
<a name="line267"></a>      if (result == goog.ui.AbstractSpellChecker.AsyncResult.PENDING) {
<a name="line268"></a>        this.textArrayIndex_ = i + 1;
<a name="line269"></a>        goog.Timer.callOnce(this.boundContinueAsyncFn_);
<a name="line270"></a>        return result;
<a name="line271"></a>      }
<a name="line272"></a>    } else {
<a name="line273"></a>      this.processRange(this.overlay_, text);
<a name="line274"></a>    }
<a name="line275"></a>  }
<a name="line276"></a>
<a name="line277"></a>  this.textArray_ = [];
<a name="line278"></a>  this.textArrayProcess_ = [];
<a name="line279"></a>
<a name="line280"></a>  return goog.ui.AbstractSpellChecker.AsyncResult.DONE;
<a name="line281"></a>};
<a name="line282"></a>
<a name="line283"></a>
<a name="line284"></a>/**
<a name="line285"></a> * Breaks text into included and excluded ranges using the marker RegExp
<a name="line286"></a> * supplied by the caller.
<a name="line287"></a> *
<a name="line288"></a> * @param {string} text text to process.
<a name="line289"></a> * @private
<a name="line290"></a> */
<a name="line291"></a>goog.ui.PlainTextSpellChecker.prototype.initTextArray_ = function(text) {
<a name="line292"></a>  if (!this.excludeMarker) {
<a name="line293"></a>    this.textArray_ = [text];
<a name="line294"></a>    this.textArrayProcess_ = [true];
<a name="line295"></a>    return;
<a name="line296"></a>  }
<a name="line297"></a>
<a name="line298"></a>  this.textArray_ = [];
<a name="line299"></a>  this.textArrayProcess_ = [];
<a name="line300"></a>  this.excludeMarker.lastIndex = 0;
<a name="line301"></a>  var stringSegmentStart = 0;
<a name="line302"></a>  var result;
<a name="line303"></a>  while (result = this.excludeMarker.exec(text)) {
<a name="line304"></a>    if (result[0].length == 0) {
<a name="line305"></a>      break;
<a name="line306"></a>    }
<a name="line307"></a>    var excludedRange = result[0];
<a name="line308"></a>    var includedRange = text.substr(stringSegmentStart, result.index -
<a name="line309"></a>        stringSegmentStart);
<a name="line310"></a>    if (includedRange) {
<a name="line311"></a>      this.textArray_.push(includedRange);
<a name="line312"></a>      this.textArrayProcess_.push(true);
<a name="line313"></a>    }
<a name="line314"></a>    this.textArray_.push(excludedRange);
<a name="line315"></a>    this.textArrayProcess_.push(false);
<a name="line316"></a>    stringSegmentStart = this.excludeMarker.lastIndex;
<a name="line317"></a>  }
<a name="line318"></a>
<a name="line319"></a>  var leftoverText = text.substr(stringSegmentStart);
<a name="line320"></a>  if (leftoverText) {
<a name="line321"></a>    this.textArray_.push(leftoverText);
<a name="line322"></a>    this.textArrayProcess_.push(true);
<a name="line323"></a>  }
<a name="line324"></a>};
<a name="line325"></a>
<a name="line326"></a>
<a name="line327"></a>/**
<a name="line328"></a> * Starts asynchrnonous spell checking.
<a name="line329"></a> *
<a name="line330"></a> * @param {string} text text to process.
<a name="line331"></a> * @private
<a name="line332"></a> */
<a name="line333"></a>goog.ui.PlainTextSpellChecker.prototype.checkAsync_ = function(text) {
<a name="line334"></a>  this.initializeAsyncMode();
<a name="line335"></a>  this.initTextArray_(text);
<a name="line336"></a>  this.textArrayIndex_ = 0;
<a name="line337"></a>  if (this.spellCheckLoop_() ==
<a name="line338"></a>      goog.ui.AbstractSpellChecker.AsyncResult.PENDING) {
<a name="line339"></a>    return;
<a name="line340"></a>  }
<a name="line341"></a>  this.finishAsyncProcessing();
<a name="line342"></a>  this.finishCheck_();
<a name="line343"></a>};
<a name="line344"></a>
<a name="line345"></a>
<a name="line346"></a>/**
<a name="line347"></a> * Continues asynchrnonous spell checking.
<a name="line348"></a> * @private
<a name="line349"></a> */
<a name="line350"></a>goog.ui.PlainTextSpellChecker.prototype.continueAsync_ = function() {
<a name="line351"></a>  // First finish with the current segment.
<a name="line352"></a>  var result = this.continueAsyncProcessing();
<a name="line353"></a>  if (result == goog.ui.AbstractSpellChecker.AsyncResult.PENDING) {
<a name="line354"></a>    goog.Timer.callOnce(this.boundContinueAsyncFn_);
<a name="line355"></a>    return;
<a name="line356"></a>  }
<a name="line357"></a>  if (this.spellCheckLoop_() ==
<a name="line358"></a>      goog.ui.AbstractSpellChecker.AsyncResult.PENDING) {
<a name="line359"></a>    return;
<a name="line360"></a>  }
<a name="line361"></a>  this.finishAsyncProcessing();
<a name="line362"></a>  this.finishCheck_();
<a name="line363"></a>};
<a name="line364"></a>
<a name="line365"></a>
<a name="line366"></a>/**
<a name="line367"></a> * Processes word.
<a name="line368"></a> *
<a name="line369"></a> * @param {Node} node Node containing word.
<a name="line370"></a> * @param {string} word Word to process.
<a name="line371"></a> * @param {goog.spell.SpellCheck.WordStatus} status Status of word.
<a name="line372"></a> * @protected
<a name="line373"></a> */
<a name="line374"></a>goog.ui.PlainTextSpellChecker.prototype.processWord = function(node, word,
<a name="line375"></a>                                                                status) {
<a name="line376"></a>  node.appendChild(this.createWordElement_(word, status));
<a name="line377"></a>};
<a name="line378"></a>
<a name="line379"></a>
<a name="line380"></a>/**
<a name="line381"></a> * Processes range of text - recognized words and separators.
<a name="line382"></a> *
<a name="line383"></a> * @param {Node} node Node containing separator.
<a name="line384"></a> * @param {string} text text to process.
<a name="line385"></a> * @protected
<a name="line386"></a> */
<a name="line387"></a>goog.ui.PlainTextSpellChecker.prototype.processRange = function(node, text) {
<a name="line388"></a>  this.endOfLineMatcher_.lastIndex = 0;
<a name="line389"></a>  var result;
<a name="line390"></a>  while (result = this.endOfLineMatcher_.exec(text)) {
<a name="line391"></a>    if (result[0].length == 0) {
<a name="line392"></a>      break;
<a name="line393"></a>    }
<a name="line394"></a>    node.appendChild(this.getDomHelper().createTextNode(result[1]));
<a name="line395"></a>    if (result[2]) {
<a name="line396"></a>      node.appendChild(this.getDomHelper().createElement(&#39;br&#39;));
<a name="line397"></a>    }
<a name="line398"></a>  }
<a name="line399"></a>};
<a name="line400"></a>
<a name="line401"></a>
<a name="line402"></a>/**
<a name="line403"></a> * Hides correction UI.
<a name="line404"></a> */
<a name="line405"></a>goog.ui.PlainTextSpellChecker.prototype.resume = function() {
<a name="line406"></a>  var wasVisible = this.isVisible_;
<a name="line407"></a>
<a name="line408"></a>  goog.ui.PlainTextSpellChecker.superClass_.resume.call(this);
<a name="line409"></a>
<a name="line410"></a>  goog.style.showElement(this.overlay_, false);
<a name="line411"></a>  goog.style.showElement(this.getElement(), true);
<a name="line412"></a>  this.getElement().readOnly = false;
<a name="line413"></a>
<a name="line414"></a>  if (wasVisible) {
<a name="line415"></a>    this.getElement().value = goog.dom.getRawTextContent(this.overlay_);
<a name="line416"></a>    this.overlay_.innerHTML = &#39;&#39;;
<a name="line417"></a>
<a name="line418"></a>    var eh = this.eventHandler_;
<a name="line419"></a>    eh.unlisten(this.overlay_, goog.events.EventType.CLICK, this.onWordClick_);
<a name="line420"></a>    eh.unlisten(/** @type {goog.events.KeyHandler} */ (this.keyHandler_),
<a name="line421"></a>        goog.events.KeyHandler.EventType.KEY, this.handleOverlayKeyEvent);
<a name="line422"></a>
<a name="line423"></a>    var win = goog.dom.getWindow(this.getDomHelper().getDocument()) || window;
<a name="line424"></a>    eh.unlisten(win, goog.events.EventType.RESIZE, this.onWindowResize_);
<a name="line425"></a>  }
<a name="line426"></a>};
<a name="line427"></a>
<a name="line428"></a>
<a name="line429"></a>/**
<a name="line430"></a> * Returns desired element properties for the specified status.
<a name="line431"></a> *
<a name="line432"></a> * @param {goog.spell.SpellCheck.WordStatus} status Status of word.
<a name="line433"></a> * @return {Object} Properties to apply to word element.
<a name="line434"></a> * @protected
<a name="line435"></a> */
<a name="line436"></a>goog.ui.PlainTextSpellChecker.prototype.getElementProperties =
<a name="line437"></a>    function(status) {
<a name="line438"></a>  if (status == goog.spell.SpellCheck.WordStatus.INVALID) {
<a name="line439"></a>    return {&#39;class&#39;: this.invalidWordClassName};
<a name="line440"></a>  } else if (status == goog.spell.SpellCheck.WordStatus.CORRECTED) {
<a name="line441"></a>    return {&#39;class&#39;: this.correctedWordClassName};
<a name="line442"></a>  }
<a name="line443"></a>  return {&#39;class&#39;: &#39;&#39;};
<a name="line444"></a>};
<a name="line445"></a>
<a name="line446"></a>
<a name="line447"></a>/**
<a name="line448"></a> * Handles the click events.
<a name="line449"></a> *
<a name="line450"></a> * @param {goog.events.BrowserEvent} event Event object.
<a name="line451"></a> * @private
<a name="line452"></a> */
<a name="line453"></a>goog.ui.PlainTextSpellChecker.prototype.onWordClick_ = function(event) {
<a name="line454"></a>  if (event.target.className == this.invalidWordClassName ||
<a name="line455"></a>      event.target.className == this.correctedWordClassName) {
<a name="line456"></a>    this.showSuggestionsMenu(/** @type {Element} */ (event.target), event);
<a name="line457"></a>
<a name="line458"></a>    // Prevent document click handler from closing the menu.
<a name="line459"></a>    event.stopPropagation();
<a name="line460"></a>  }
<a name="line461"></a>};
<a name="line462"></a>
<a name="line463"></a>
<a name="line464"></a>/**
<a name="line465"></a> * Handles window resize events.
<a name="line466"></a> *
<a name="line467"></a> * @param {goog.events.BrowserEvent} event Event object.
<a name="line468"></a> * @private
<a name="line469"></a> */
<a name="line470"></a>goog.ui.PlainTextSpellChecker.prototype.onWindowResize_ = function(event) {
<a name="line471"></a>  var win = goog.dom.getWindow(this.getDomHelper().getDocument()) || window;
<a name="line472"></a>  var size = goog.dom.getViewportSize(win);
<a name="line473"></a>
<a name="line474"></a>  if (size.width != this.winSize_.width ||
<a name="line475"></a>      size.height != this.winSize_.height) {
<a name="line476"></a>    goog.style.showElement(this.overlay_, false);
<a name="line477"></a>    goog.style.showElement(this.getElement(), true);
<a name="line478"></a>
<a name="line479"></a>    // IE requires a slight delay, allowing the resize operation to take effect.
<a name="line480"></a>    if (goog.userAgent.IE) {
<a name="line481"></a>      goog.Timer.callOnce(this.resizeOverlay_, 100, this);
<a name="line482"></a>    } else {
<a name="line483"></a>      this.resizeOverlay_();
<a name="line484"></a>    }
<a name="line485"></a>    this.winSize_ = size;
<a name="line486"></a>  }
<a name="line487"></a>};
<a name="line488"></a>
<a name="line489"></a>
<a name="line490"></a>/**
<a name="line491"></a> * Resizes overlay to match the size of the bound element then displays the
<a name="line492"></a> * overlay. Helper for {@link #onWindowResize_}.
<a name="line493"></a> *
<a name="line494"></a> * @private
<a name="line495"></a> */
<a name="line496"></a>goog.ui.PlainTextSpellChecker.prototype.resizeOverlay_ = function() {
<a name="line497"></a>   this.positionOverlay_();
<a name="line498"></a>   goog.style.showElement(this.getElement(), false);
<a name="line499"></a>   goog.style.showElement(this.overlay_, true);
<a name="line500"></a>};
<a name="line501"></a>
<a name="line502"></a>
<a name="line503"></a>/**
<a name="line504"></a> * Updates the position and size of the overlay to match the original element.
<a name="line505"></a> *
<a name="line506"></a> * @private
<a name="line507"></a> */
<a name="line508"></a>goog.ui.PlainTextSpellChecker.prototype.positionOverlay_ = function() {
<a name="line509"></a>  goog.style.setPosition(
<a name="line510"></a>      this.overlay_, goog.style.getPosition(this.getElement()));
<a name="line511"></a>  goog.style.setSize(this.overlay_, goog.style.getSize(this.getElement()));
<a name="line512"></a>};
<a name="line513"></a>
<a name="line514"></a>
<a name="line515"></a>/** @inheritDoc */
<a name="line516"></a>goog.ui.PlainTextSpellChecker.prototype.disposeInternal = function() {
<a name="line517"></a>  goog.ui.PlainTextSpellChecker.superClass_.disposeInternal.call(this);
<a name="line518"></a>  this.getDomHelper().removeNode(this.overlay_);
<a name="line519"></a>  delete this.overlay_;
<a name="line520"></a>  delete this.boundContinueAsyncFn_;
<a name="line521"></a>  delete this.endOfLineMatcher_;
<a name="line522"></a>};
<a name="line523"></a>
<a name="line524"></a>
<a name="line525"></a>/**
<a name="line526"></a> * Specify ARIA roles and states as appropriate.
<a name="line527"></a> * @private
<a name="line528"></a> */
<a name="line529"></a>goog.ui.PlainTextSpellChecker.prototype.initAccessibility_ = function() {
<a name="line530"></a>  goog.dom.a11y.setRole(this.overlay_, &#39;region&#39;);
<a name="line531"></a>  goog.dom.a11y.setState(this.overlay_, &#39;live&#39;, &#39;assertive&#39;);
<a name="line532"></a>  this.overlay_.tabIndex = 0;
<a name="line533"></a>
<a name="line534"></a>  /** @desc Title for Spell Checker&#39;s overlay.*/
<a name="line535"></a>  var MSG_SPELLCHECKER_OVERLAY_TITLE = goog.getMsg(&#39;Spell Checker&#39;);
<a name="line536"></a>  this.overlay_.title = MSG_SPELLCHECKER_OVERLAY_TITLE;
<a name="line537"></a>};
<a name="line538"></a>
<a name="line539"></a>
<a name="line540"></a>/**
<a name="line541"></a> * Handles key down for overlay.
<a name="line542"></a> * @param {goog.events.BrowserEvent} e The browser event.
<a name="line543"></a> * @return {boolean} The handled value.
<a name="line544"></a> */
<a name="line545"></a>goog.ui.PlainTextSpellChecker.prototype.handleOverlayKeyEvent = function(e) {
<a name="line546"></a>  var handled = false;
<a name="line547"></a>  switch (e.keyCode) {
<a name="line548"></a>    case goog.events.KeyCodes.RIGHT:
<a name="line549"></a>      if (e.ctrlKey) {
<a name="line550"></a>        handled = this.navigate_(goog.ui.AbstractSpellChecker.Direction.NEXT);
<a name="line551"></a>      }
<a name="line552"></a>      break;
<a name="line553"></a>
<a name="line554"></a>    case goog.events.KeyCodes.LEFT:
<a name="line555"></a>      if (e.ctrlKey) {
<a name="line556"></a>        handled = this.navigate_(
<a name="line557"></a>            goog.ui.AbstractSpellChecker.Direction.PREVIOUS);
<a name="line558"></a>      }
<a name="line559"></a>      break;
<a name="line560"></a>
<a name="line561"></a>    case goog.events.KeyCodes.DOWN:
<a name="line562"></a>      if (this.focusedElementId_) {
<a name="line563"></a>        var el = this.getDomHelper().getElement(this.makeElementId(
<a name="line564"></a>            this.focusedElementId_));
<a name="line565"></a>        if (el) {
<a name="line566"></a>          var position = goog.style.getPosition(el);
<a name="line567"></a>          var size = goog.style.getSize(el);
<a name="line568"></a>          position.x += size.width / 2;
<a name="line569"></a>          position.y += size.height / 2;
<a name="line570"></a>          handled = this.showSuggestionsMenu(el, position);
<a name="line571"></a>        }
<a name="line572"></a>      }
<a name="line573"></a>      break;
<a name="line574"></a>  }
<a name="line575"></a>
<a name="line576"></a>  if (handled) {
<a name="line577"></a>    e.preventDefault();
<a name="line578"></a>  }
<a name="line579"></a>
<a name="line580"></a>  return handled;
<a name="line581"></a>};
<a name="line582"></a>
<a name="line583"></a>
<a name="line584"></a>/**
<a name="line585"></a> * Navigate keyboard focus in the given direction.
<a name="line586"></a> *
<a name="line587"></a> * @param {goog.ui.AbstractSpellChecker.Direction} direction The direction to
<a name="line588"></a> *     navigate in.
<a name="line589"></a> * @return {boolean} Whether the action is handled here.  If not handled
<a name="line590"></a> *     here, the initiating event may be propagated.
<a name="line591"></a> * @private
<a name="line592"></a> */
<a name="line593"></a>goog.ui.PlainTextSpellChecker.prototype.navigate_ = function(direction) {
<a name="line594"></a>  var handled = false;
<a name="line595"></a>  var previous = direction == goog.ui.AbstractSpellChecker.Direction.PREVIOUS;
<a name="line596"></a>  var lastId = goog.ui.AbstractSpellChecker.nextId_;
<a name="line597"></a>  var focusedId = this.focusedElementId_;
<a name="line598"></a>
<a name="line599"></a>  var el;
<a name="line600"></a>  do {
<a name="line601"></a>    focusedId += previous ? -1 : 1;
<a name="line602"></a>    if (focusedId &lt; 1 || focusedId &gt; lastId) {
<a name="line603"></a>      focusedId = 0;
<a name="line604"></a>      break;
<a name="line605"></a>    }
<a name="line606"></a>  } while (!(el = this.getElementById(focusedId)));
<a name="line607"></a>
<a name="line608"></a>  if (el) {
<a name="line609"></a>    el.focus();
<a name="line610"></a>    this.focusedElementId_ = focusedId;
<a name="line611"></a>    handled = true;
<a name="line612"></a>  }
<a name="line613"></a>
<a name="line614"></a>  return handled;
<a name="line615"></a>};
<a name="line616"></a>
<a name="line617"></a>
<a name="line618"></a>/**
<a name="line619"></a> * Handles correction menu actions.
<a name="line620"></a> *
<a name="line621"></a> * @param {goog.events.Event} event Action event.
<a name="line622"></a> * @protected
<a name="line623"></a> */
<a name="line624"></a>goog.ui.PlainTextSpellChecker.prototype.onCorrectionAction = function(event) {
<a name="line625"></a>  goog.ui.PlainTextSpellChecker.superClass_.onCorrectionAction.call(this,
<a name="line626"></a>      event);
<a name="line627"></a>
<a name="line628"></a>  // In case of editWord base class has already set the focus (on the input),
<a name="line629"></a>  // otherwise set the focus back on the word.
<a name="line630"></a>  if (event.target != this.menuEdit_) {
<a name="line631"></a>    this.reFocus_();
<a name="line632"></a>  }
<a name="line633"></a>};
<a name="line634"></a>
<a name="line635"></a>
<a name="line636"></a>/**
<a name="line637"></a> * Handles blur on the menu.
<a name="line638"></a> * @param {goog.events.BrowserEvent} event Blur event.
<a name="line639"></a> * @private
<a name="line640"></a> */
<a name="line641"></a>goog.ui.PlainTextSpellChecker.prototype.onCorrectionBlur_ = function(event) {
<a name="line642"></a>  this.reFocus_();
<a name="line643"></a>};
<a name="line644"></a>
<a name="line645"></a>
<a name="line646"></a>/**
<a name="line647"></a> * Sets the focus back on the previously focused word element.
<a name="line648"></a> * @private
<a name="line649"></a> */
<a name="line650"></a>goog.ui.PlainTextSpellChecker.prototype.reFocus_ = function() {
<a name="line651"></a>  var el = this.getElementById(this.focusedElementId_);
<a name="line652"></a>  if (el) {
<a name="line653"></a>    el.focus();
<a name="line654"></a>  } else {
<a name="line655"></a>    this.overlay_.focus();
<a name="line656"></a>  }
<a name="line657"></a>};
</pre>


</body>
</html>
